<#import "/META-INF/spring.ftl" as spring />
<#import "/META-INF/mspring.ftl" as mspring />
<#include "/WEB-INF/views/admin/inc/header.ftl" />

<link rel="stylesheet" type="text/css" href="${base}/script/lhgcalendar/skins/lhgcalendar.css">
<script type="text/javascript" src="${base}/script/lhgcalendar/lhgcalendar.min.js"></script>
<form id="spiderPostForm" name="spiderPostForm" action="${base}/admin/spider/post/list" method="POST">
	<@spring.bind "spiderPost" />
	<table>
		<tr>
			<td class="fieldlabel">状态</td>
			<td>
				<select name="posted" onchange="$('#spiderPostForm').submit()">
					<option value="">--请选择--</option>
					<option value="1" <#if spiderPost.posted?exists && spiderPost.posted>selected="selected"</#if>>已发布</option>
					<option value="0" <#if spiderPost.posted?exists && !spiderPost.posted>selected="selected"</#if>>未发布</option>
				</select>
			</td>
			
			<td class="fieldlabel">标题</td>
			<td>
				<@spring.formInput path="spiderPost.title" attributes='class="textinput"' />
			</td>
			
			<td class="fieldlabel">采集规则</td>
			<td>
				<@spring.formSingleSelect path="spiderPost.spiderRule" options=spiderRules has_default=true attributes='class="textinput"' />
			</td>
			
			<td class="fieldlabel">发布规则</td>
			<td>
				<@spring.formSingleSelect path="spiderPost.publishRule" options=publishRules has_default=true attributes='class="textinput"' />
			</td>
			
			<td class="fieldlabel">抓取时间</td>
			<td>
				<input type="text" class="textinput" name="createTimeBeg" id="createTimeBeg" readonly="readonly" value="${createTimeBeg?default("")}" style="width:85px;" /> - 
				<input type="text" class="textinput" name="createTimeEnd" id="createTimeEnd" readonly="readonly" value="${createTimeEnd?default("")}" style="width:85px;" />
			</td>
			
			<td><input type="submit" class="btn" value=" 查 询 " /></td>
		</tr>
	</table>
		
	<@spring.bind "spiderPostPage" />
	<@spring.formHiddenInput path="spiderPostPage.pageNo" />
	<@spring.formHiddenInput path="spiderPostPage.totalPages" />
	<@spring.formHiddenInput path="spiderPostPage.totalCount" />
	
	<@spring.formHiddenInput path="spiderPostPage.sortEnable" />
	<@spring.formHiddenInput path="spiderPostPage.sort.field" />
	<@spring.formHiddenInput path="spiderPostPage.sort.order" />
	<table class="dtable" cellspacing="0" cellpadding="0">
		<tr>
			<th><input type="checkbox" onclick="mlog.form.checkAll(this, 'ids');" /></th>
			<th onclick="sortField(this, 'spiderPostForm', 'id');" id='id_sort'>编号</th>
			<th onclick="sortField(this, 'spiderPostForm', 'title');" id='title_sort'>标题</th>
			<th onclick="sortField(this, 'spiderPostForm', 'createTime');" id='createTime_sort'>采集时间</th>
			<th onclick="sortField(this, 'spiderPostForm', 'posted');" id='posted_sort'>状态</th>
			<th onclick="sortField(this, 'spiderPostForm', 'postTime');" id='createTime_sort'>发布时间</th>
			<th>操作</th>
		</tr>
		<#if spiderPostPage?exists>
			<#list spiderPostPage.result as item>
				<#assign tdClass = "odd">
				<#if item_index%2 == 0>
					<#assign tdClass = "even">
				</#if>
				<tr>
					<td class="${tdClass}"><input type="checkbox" name="ids" value="${item.id}" /></td>
					<td class="${tdClass}">${item.id} </td>
					<td class="${tdClass}"><a href="${base}/admin/spider/post/view?id=${item.id}">${item.title}</a> </td>
					<td class="${tdClass}">${item.createTime?string("yyyy-MM-dd hh:mm:ss")} </td>
					<td class="${tdClass}">${item.posted?string("<font style='color:blue;'>已发布</font>","<font style='color:red;'>未发布</font>")} </td>
					<td class="${tdClass}">${item.postTime!""} </td>
					<td class="${tdClass}"><a href="${base}/admin/spider/post/edit?id=${item.id}">修改</a></td>
				</tr>
			</#list>
		</#if>
	</table>
	<table style="width:100%;">
		<tr>
			<td>
				<input type="button" class="btn" value=" 标记为已发布 " onclick="mlog.form.confirmSubmit('spiderPostForm', '${base}/admin/spider/post/changePosted?posted=true', '确认要将选中文章标记为已发布吗？');" />
				<input type="button" class="btn" value=" 标记为未发布 " onclick="mlog.form.confirmSubmit('spiderPostForm', '${base}/admin/spider/post/changePosted?posted=false', '确认要将选中文章标记为未发布吗？');" />
				<input type="button" class="btn" value=" 删除 " onclick="mlog.form.confirmSubmit('spiderPostForm', '${base}/admin/spider/post/delete', '确认要删除选中文章吗？');" />
				<input type="button" class="btn" value=" 发布到分类： " onclick="publishPost();" />
				<input type="text" id="catalogSel" onclick="showMenu();" class="textinput" readonly="readonly"/>
				<input type="hidden" name="catalog" id="catalog"/>
				<div id="menuContent" class="menuContent" style="display:none;position: absolute; overflow:auto; background:#fff; border:1px solid #cccccc;z-index: 100;">
					<div style="display:block;float:right; padding:2px;"><input type="button" class="btn" onclick="hideMenu();" value="关闭" /></div>
					<ul id="catalogComboTree" class="ztree" style="margin-top:0; width:240px; height: 300px;"></ul>
				</div>
			</td>
			<td>
				<@mspring.pagingnavigator page=spiderPostPage form_id="spiderPostForm" />
			</td>
		</tr>
	</table>
</form>
<script type="text/javascript">
	var setting = {
		check: {
			enable: true,
			chkStyle: "radio",
			radioType: "level"
		},
		view: {
			dblClickExpand: false
		},
		data: {
			simpleData: {
				enable: true
			}
		},
		callback: {
			beforeClick: beforeClick,
			onCheck: onCheck
		}
	};
	var zNodes =[
		<#if (catalogs?exists && catalogs?size > 0)>
			<#list catalogs as catalog>
				{id:${catalog.id}, name:"${catalog.name}"}
				<#if catalog_has_next>,</#if>
			</#list>
		</#if>
	];
	function beforeClick(treeId, treeNode) {
		var zTree = $.fn.zTree.getZTreeObj("catalogComboTree");
		zTree.checkNode(treeNode, !treeNode.checked, null, true);
		return false;
	}
	function onCheck(e, treeId, treeNode) {
		var zTree = $.fn.zTree.getZTreeObj("catalogComboTree"),
		nodes = zTree.getCheckedNodes(true),
		text = nodes[0].name,
		value = nodes[0].id;
		
		$("#catalogSel").attr("value", text);
		$("#catalog").attr("value", value);
		hideMenu();
	}
	function showMenu() {
		var catalogSelObj = $("#catalogSel");
		var catalogSelOffset = $("#catalogSel").offset();
		$("#menuContent").css({left:catalogSelOffset.left + "px", bottom:catalogSelObj.outerHeight() + "px"}).slideDown("fast");

		$("body").bind("mousedown", onBodyDown);
	}
	function hideMenu() {
		$("#menuContent").fadeOut("fast");
		$("body").unbind("mousedown", onBodyDown);
	}
	function onBodyDown(event) {
		if (!(event.target.id == "menuBtn" || event.target.id == "catalogSel" || event.target.id == "menuContent" || $(event.target).parents("#menuContent").length>0)) {
			hideMenu();
		}
	}
	$(document).ready(function(){
		$.fn.zTree.init($("#catalogComboTree"), setting, zNodes);
	});
	
	//发布文章
	function publishPost(){
		var catalog = document.getElementById('catalog').value;
		if(!catalog){
			alert('请先选择发布的分类');
			return;
		}
		var ids = document.getElementsByName('ids');
		var checked = new Array();
		var j = 0;
		for(var i = 0; i < ids.length; i++){
			if(ids[i].checked){
				checked[j] = ids[i].value;
				j++; 
			}
		}
		if(checked.length < 1){
			alert('请选择要发布的文章');
			return;
		}
		if(confirm('确认要发布选中文章吗？')){
			var dialog = mlog.dialog.showModelDialog({
				title : '正在发布...',
				icon : 'loading.gif',
				content : '正在发布...',
				max : false,
				min : false,
			});
			dialog.close =  function(){
				if(confirm('确认要中止发布吗？')){
					this.hide();
					window.location.href = '${base}/admin/spider/post/list';
				}
			}
			publish(dialog, checked, catalog);
		}
	}
	
	//递归发布
	function publish(dialog, checked, catalog, current){
		if(!current){
			current = 1;
		}
		var id = checked[current - 1];
		dialog.content('正在发布文章...' + id);
		$.post('${base}/admin/spider/post/publish', {id: id, catalog: catalog}, function(response){
			var msg = '';
			if(response.success == true){
				msg = '<font color="green">成功</font>';
			}
			else {
				msg = '<font color="red">失败</font>';
			}
			dialog.content('发布文章：' + id + '...' + msg);
			
			
			if(current != checked.length){
				current++;
				publish(dialog, checked, catalog, current);
			}
			else{
				alert('全部发布完成');
				dialog.hide();
				window.location.href = '${base}/admin/spider/post/list';
			}
		})
	}
</script>
<script type="text/javascript">
	$(document).ready(function(){
		$("#createTimeBeg").calendar({ maxDate:'#createTimeEnd', format:'yyyy-MM-dd HH:mm:ss', targetFormat:'yyyy-MM-dd HH:mm:ss' });
		$("#createTimeEnd").calendar({ minDate:'#createTimeBeg', format:'yyyy-MM-dd HH:mm:ss', targetFormat:'yyyy-MM-dd HH:mm:ss' });
	});
</script>
<#include "/WEB-INF/views/admin/inc/footer.ftl" />